package org.example.route.controller.UserController;


import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Select;
import org.example.route.context.BaseContext;
import org.example.route.model.dto.AIChatDTO;
import org.example.route.model.dto.CityQueryDTO;
import org.example.route.model.pojo.Result;
import org.example.route.model.vo.AIChatVO;
import org.example.route.service.CityService;
import org.example.route.service.ProvinceService;
import org.example.route.service.StationService;
import org.example.route.service.impl.CityServiceImpl;
import org.example.route.service.impl.ProvinceServiceImpl;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.AbstractChatMemoryAdvisor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequiredArgsConstructor
@RequestMapping("/user/ai")
@Tag(name = "AI接口管理")
@Slf4j
public class AIController {



    private final ChatClient chatClient;
    private final ProvinceService provinceService;
    private final CityService cityService;
    private final StationService stationService;

    @PostMapping(value = "/chat",produces = "application/json;charset=utf-8")
    @Operation(summary = "云途灵智能AI助手接口")
    public Result<AIChatVO> chat(@RequestBody AIChatDTO aiChatDTO)
    {

        log.info("云途灵智能AI助手接口,{}", aiChatDTO);
        AIChatVO aiChatVO = new AIChatVO();
        aiChatVO.setAnswer(chatClient.prompt().tools(provinceService, cityService,stationService
                )
                .advisors(advisorSpec -> advisorSpec.param(AbstractChatMemoryAdvisor.CHAT_MEMORY_CONVERSATION_ID_KEY, BaseContext.getCurrentId().toString()))//添加会话记忆功能
                .user(aiChatDTO.getPrompt())
                .call()
                .content());
        System.out.println(aiChatVO.getAnswer().toString());
        return Result.success(aiChatVO);
    }





}
